package com.iwop.moto.controller;

import com.iwop.moto.comm.Comm;
import com.iwop.moto.comm.ErrCode;
import com.iwop.moto.model.*;
import com.iwop.moto.service.MGPOrderService;
import com.iwop.moto.service.StoreService;
import com.iwop.moto.utils.stringUtil.ObjectId;
import com.iwop.moto.utils.stringUtil.SequenceUtil;
import com.iwop.moto.utils.stringUtil.StringUtils;
import com.wordnik.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * Created by mobom on 2016/12/28.
 */
@RequestMapping("mgporder")
@Controller
public class MGPOrderController extends BaseController {

    @Autowired
    private MGPOrderService mgpOrderService;

    @Autowired
    private StoreService storeService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public ResultModel addMGPOrder(@ApiParam(name = "money", value = "金额", defaultValue = "")
                                        @RequestParam(value = "money") double money,
                                        @ApiParam(name = "store_id", value = "店铺id", defaultValue = "")
                                        @RequestParam(value = "store_id") String store_id,
                                        @ApiParam(name = "content", value = "说明",required = false, defaultValue = "")
                                        @RequestParam(value = "content",required = false) String content,
                                        @ApiParam(name = "user_id", value = "用户id",required = false, defaultValue = "")
                                        @RequestParam(value = "user_id",required = false) String user_id,
                                        @ApiParam(name = "pay_type", value = "支付端类型", defaultValue = "")
                                        @RequestParam(value = "pay_type") String pay_type,
                                   HttpServletRequest request,
                                   HttpServletResponse response) throws IOException {
            MGPOrder mgpOrder=new MGPOrder();
            mgpOrder.setSid(ObjectId.UUID());
            mgpOrder.setStoreId(store_id);
            mgpOrder.setAddTime(new Date());
            mgpOrder.setUserId(user_id);
            mgpOrder.setMoney(money);
            mgpOrder.setContent(content);
            mgpOrder.setOrderNo(SequenceUtil.Serial("Ho"));
            mgpOrder.setPayType(pay_type);
            mgpOrder.setStatusTime(new Date());
            mgpOrder.setStatusFlag("0");
            int result=mgpOrderService.insetMGPOrder(mgpOrder);
            if(result==0){
                return getErr(ErrCode.ERROR_SYSTEM);
            }else {

                if(pay_type.equals("WXPAY")){
                    String url = Comm.HTTP_URL + "/api/wx/h5pay/gporder"+"?out_trade_no="+mgpOrder.getOrderNo()+"&store_id="+mgpOrder.getStoreId()+"&money="+mgpOrder.getMoney();
                    System.out.println(url);
                    response.sendRedirect(url);
                    return null;
                }else if(pay_type.equals("APPPAY")){
                    SuperRecordModel storeBySid = storeService.getStoreBySid(store_id);
                    String user_id1 = storeBySid.getString("user_id");
                    mgpOrder.setSellerUserId(user_id1);
                    return getSucceed(mgpOrder,ErrCode.SUCCEED);
                }else {
                    String url = Comm.HTTP_URL + "/api/alipay/h5/pay"+"?out_trade_no="+mgpOrder.getOrderNo()+"&store_id="+mgpOrder.getStoreId()+"&money="+mgpOrder.getMoney();
                    response.sendRedirect(url);
                    return null;
                }
            }
    }

    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ResponseBody
    public ResultModel mGPOrderDetail(@ApiParam(name = "order_no", value = "订单id", defaultValue = "")
                                        @RequestParam(value = "order_no") String order_no
                                        ) {
        try {
            MGPOrder mgpOrder=mgpOrderService.findOneBySid(order_no);
            return getSucceed(mgpOrder,ErrCode.SUCCEED);
        } catch (Exception e) {
            e.printStackTrace();
            return getErr(ErrCode.SUCCEED);
        }
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public ResultModel orderPurchaseList(@ApiParam(name = "store_id", value = "店铺id", defaultValue = "")
                                         @RequestParam(value = "store_id") String store_id,
                                         @ApiParam(name = "rows", value = "行数", defaultValue = "")
                                         @RequestParam(value = "rows") int rows,
                                         @ApiParam(name = "pages", value = "页码", defaultValue = "")
                                         @RequestParam(value = "pages") int pages
    ) {
        try {
            if(pages<1){
                pages=1;
            }
            if(rows<1){
                rows=1;
            }
            int offset = (pages - 1) * rows;
            Long aLong = mgpOrderService.countMGPOrderByStoreId(store_id);
            List<MGPOrder> mgpOrder=mgpOrderService.findGPOrderByStoreId(store_id,rows,offset);
            return getSucceed(mgpOrder,ErrCode.SUCCEED,aLong+"");
        } catch (Exception e) {
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }
}
